package sii.commons;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



public class Utils {

    private static Log log;

    private static void init() {
	if (log == null)
	    log = LogFactory.getLog(Utils.class);
    }

    public static String time(long mills) {

	long hours = TimeUnit.MILLISECONDS.toHours(mills);

	long minutes = TimeUnit.MILLISECONDS.toMinutes(mills)
		- TimeUnit.HOURS.toMinutes(hours);

	long seconds = TimeUnit.MILLISECONDS.toSeconds(mills)
		- TimeUnit.HOURS.toSeconds(hours)
		- TimeUnit.MINUTES.toSeconds(minutes);

	long ms = mills - TimeUnit.HOURS.toMillis(hours)
		- TimeUnit.MINUTES.toMillis(minutes)
		- TimeUnit.SECONDS.toMillis(seconds);
	if (hours > 0)
	    if (hours==1){
		return String.format("%d ora, %d min, %d sec, %d ms", hours,
			    minutes, seconds, ms);
	    }else{
		return String.format("%d ore, %d min, %d sec, %d ms", hours,
			    minutes, seconds, ms);
	    }
	    
	else if (minutes > 0) {
	    return String.format("%d min, %d sec, %d ms", minutes, seconds, ms);
	} else if (seconds > 0) {
	    return String.format("%d sec, %d ms", seconds, ms);
	} else {
	    return String.format("%d \tms", ms);
	}
    }

    public static int[] sortAsc(Integer[] array) {
	final int[] temp = new int[array.length];
	System.arraycopy(array, 0, temp, 0, array.length);
	Arrays.sort(temp, 0, temp.length);
	return temp;
    }

    public static Integer[] getArrayIntAsc(Set<Integer> set) {
	final Integer[] temp = new Integer[set.size()];
	System.arraycopy(set.toArray(), 0, temp, 0, set.size());
	Arrays.sort(temp, 0, temp.length);
	return temp;
    }

    public static byte[] serialize(Serializable myObj) throws CommonException {
	init();
	ByteArrayOutputStream baos;
	GZIPOutputStream gzipOut;
	ObjectOutputStream objectOut;
	byte[] bytes = null;
	try {
	    baos = new ByteArrayOutputStream();
	    gzipOut = new GZIPOutputStream(baos);
	    objectOut = new ObjectOutputStream(gzipOut);
	    objectOut.writeObject(myObj);
	    objectOut.close();
	    bytes = baos.toByteArray();
	} catch (Exception e) {
	    String msg = "Errore durante la serializzazzione dell'oggetto: "
		    + e.getMessage();
	    log.error(msg, e);
	    throw new CommonException(e);
	}

	return bytes;
    }

    private static Object unSerialize(byte[] bytes) throws CommonException {
	init();
	Object myObj = null;
	ByteArrayInputStream bais;

	GZIPInputStream gzipIn;

	ObjectInputStream objectIn;
	try {
	    bais = new ByteArrayInputStream(bytes);
	    gzipIn = new GZIPInputStream(bais);
	    objectIn = new ObjectInputStream(gzipIn);
	    myObj = objectIn.readObject();
	    objectIn.close();
	} catch (Exception e) {
	    String msg = "Errore durante la deserializzazzione dell'oggetto: "
		    + e.getMessage();
	    log.error(msg, e);
	    throw new CommonException(e);
	}
	return myObj;
    }
    
    public static RatingItem getRatingItem(byte[] bytes) throws CommonException{
	return (RatingItem) unSerialize(bytes);
    }
    
    public static SimilarityItem getSimilarityItem(byte[] bytes) throws CommonException{
	return (SimilarityItem) unSerialize(bytes);
    }
    
    public static SumSize getSumSize(byte[] bytes) throws CommonException{
	return (SumSize) unSerialize(bytes);
    }
}
